package com.tianji.config;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;

@Slf4j
@Getter
@Configuration
@RequiredArgsConstructor
public class SystemPromptConfig {

    private final NacosConfigManager nacosConfigManager;
    private final AIProperties aiProperties;

    // 使用原子引用，保证线程安全
    private final AtomicReference<String> chatSystemMessage = new AtomicReference<>();
    // 总结标题的系统提示词
    private final AtomicReference<String> summarySystemMessage = new AtomicReference<>();
    // 路由智能体
    private final AtomicReference<String> routeAgentSystemMessage = new AtomicReference<>();
    //推荐分析智能体
    private final AtomicReference<String> recommendAgentSystemMessage = new AtomicReference<>();
    //课程购买智能体
    private final AtomicReference<String> buyAgentSystemMessage = new AtomicReference<>();
    //课程咨询智能体
    private final AtomicReference<String> consultAgentSystemMessage = new AtomicReference<>();
    //课程讲解
    private final AtomicReference<String> knowledgeAgentSystemMessage = new AtomicReference<>();


    @PostConstruct // 初始化时加载配置
    public void init() {
        // 读取配置文件
        loadConfig(aiProperties.getSystem().getChat(), chatSystemMessage);
        //设置标题
        loadConfig(aiProperties.getSystem().getSummary(), summarySystemMessage);
        //路由分析
        loadConfig(aiProperties.getSystem().getRouteAgent(), routeAgentSystemMessage);
        //推荐分析
        loadConfig(aiProperties.getSystem().getRecommendAgent(), recommendAgentSystemMessage);
        //课程购买
        loadConfig(aiProperties.getSystem().getBuyAgent(), buyAgentSystemMessage);
        //课程咨询
        loadConfig(aiProperties.getSystem().getConsultAgent(), consultAgentSystemMessage);
        //课程讲解
        loadConfig(aiProperties.getSystem().getKnowledgeAgent(), knowledgeAgentSystemMessage);
    }

    private void loadConfig(AIProperties.System.Chat chatConfig, AtomicReference<String> target) {
        try {
            var dataId = chatConfig.getDataId();
            var group = chatConfig.getGroup();
            var timeoutMs = chatConfig.getTimeoutMs();

            doLoadConfig(target, dataId, group, timeoutMs);
        } catch (Exception e) {
            log.error("加载配置失败", e);
        }
    }

    private void doLoadConfig(AtomicReference<String> target, String dataId, String group, long timeoutMs) throws NacosException {
        var config = nacosConfigManager.getConfigService().getConfig(dataId, group, timeoutMs);
        target.set(config);
        log.info("读取{}成功，内容为：{}", target, config);

        nacosConfigManager.getConfigService().addListener(dataId, group, new Listener() {
            @Override
            public Executor getExecutor() {
                return null;
            }

            @Override
            public void receiveConfigInfo(String info) {
                target.set(info);
                log.info("更新{}成功，内容为：{}", target, info);
            }
        });
    }

}